package com.lee.pullrefresh.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lee.pullrefresh.R;

import java.util.LinkedList;

/**
 * Created by Administrator on 2017/1/20 0020.
 */

public class LoadMoreRecyclerView extends PullToRefreshBase<RecyclerView> {


    private boolean mIsBegin = false;

    private RecyclerView mRecyclerView;

//    private LinkedList<String> mData = new LinkedList<>();

    private boolean hasMoreData = true;

    public LoadMoreRecyclerView(Context context) {
        this(context, null);
    }

    public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
//        init(context);
    }

    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        mRecyclerView = new RecyclerView(context);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (isScrollLoadEnabled() && hasMoreData ){
                    if (newState == RecyclerView.SCROLL_STATE_IDLE){
                        if (isReadyForPullUp()){
                            startLoading();
                        }
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                if (mListener != null)
//                    mListener.onLoadMoreListener(isLastItemVisible());
            }
        });
//        adapter = new LoadMoreAdapter(context);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
//        mRecyclerView.setAdapter(adapter);

        return mRecyclerView;
    }

    @Override
    protected boolean isReadyForPullDown() {
        return isFirstItemVisible();
    }

    @Override
    protected boolean isReadyForPullUp() {
        return isLastItemVisible();
    }

    public boolean isFirstItemVisible() {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
        if (null == linearLayoutManager) {
            return true;
        }
        int mostTop = (linearLayoutManager.findFirstVisibleItemPosition() == 0) ? linearLayoutManager.getChildAt(0).getTop() : -1;
        if (mostTop >= 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断最后一个item是否 显示
     *
     * @return
     */
    public boolean isLastItemVisible() {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();

        if (null == linearLayoutManager) return true;

        final int lastItemPosition = linearLayoutManager.getItemCount() - 1;
        final int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
//        Log.e("isLastItemVisible", "child :  " + linearLayoutManager.findLastVisibleItemPosition() + "  bottom :  "  + linearLayoutManager.getChildAt(0).getBottom());
        if (lastVisiblePosition >= lastItemPosition - 1) {
            final int childIndex = lastVisiblePosition - linearLayoutManager.findFirstVisibleItemPosition();
            final int childCount = linearLayoutManager.getChildCount();
            final int index = Math.min(childCount - 1, childIndex);
            final View lastVisibleChild = linearLayoutManager.getChildAt(index);
            if (null != lastVisibleChild) {
                return lastVisibleChild.getBottom() <= mRecyclerView.getBottom();
            }
        }
        return false;
    }

//    LoadMoreAdapter adapter;


//    /**
//     * 表示是否还有更多数据
//     *
//     * @return true表示还有更多数据
//     */
//    private boolean hasMoreData() {
//        if ((null != mLoadMoreFooterLayout) && (mLoadMoreFooterLayout.getState() == ILoadingLayout.State.NO_MORE_DATA)) {
//            return false;
//        }
//
//        return true;
//    }

//    public void init(Context context) {
//        mRecyclerView = new RecyclerView(context);
//        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//
//
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                mListener.onLoadMoreListener(isLastItemVisible());
//            }
//        });
//        adapter = new LoadMoreAdapter(context);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
//        mRecyclerView.setAdapter(adapter);
//    }


//    public void setBeginLoad(boolean isBegin) {
//        this.mIsBegin = isBegin;
//
//    }
//
//    public void setData(LinkedList data) {
//        this.mData = data;
////        adapter.notifyDataSetChanged();
//    }


    private OnLoadScrollListener mListener;

    public void setHasMoreData(boolean hasMoreData) {
        this.hasMoreData = hasMoreData;
    }

    public interface OnLoadScrollListener {

        void onLoadMoreListener(boolean lastItemVisible);
    }

    public void setOnLoadScrollListener(OnLoadScrollListener listener) {
        this.mListener = listener;
    }

//    private class LoadMoreAdapter extends RecyclerView.Adapter {
//
//        private Context context;
//
//        private LayoutInflater inflater;
//
//
//        //        private static final int TYPE_HEAD = 0;
//        private static final int TYPE_FOOT = 1;
//        private static final int TYPE_CONTENT = 2;
//
//
//        public LoadMoreAdapter(Context context) {
//            inflater = LayoutInflater.from(context);
//            this.context = context;
//        }
//
//        @Override
//        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            if (TYPE_FOOT == viewType) {
//                return new FootViewHolder(inflater.inflate(R.layout.pull_to_load_footer, parent, false));
//            } else {
//                return new TestViewHolder(inflater.inflate(R.layout.view_text_item, parent, false));
//            }
//        }
//
//        @Override
//        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//            if (holder instanceof TestViewHolder) {
//                ((TestViewHolder) holder).content.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Toast.makeText(context, mData.get(position), Toast.LENGTH_SHORT).show();
//                    }
//                });
//            }
//        }
//
//        @Override
//        public int getItemCount() {
//            return mData.size() + 1;
//        }
//
//        @Override
//        public int getItemViewType(int position) {
////            if (0 == position) {
////                return TYPE_HEAD;
////            }
//            if (mData.size() == position) {
//                return TYPE_FOOT;
//            } else {
//                return TYPE_CONTENT;
//            }
//
////            return super.getItemViewType(position);
//        }
//    }
//
//
//    private class FootViewHolder extends RecyclerView.ViewHolder {
//
//        public FootViewHolder(View itemView) {
//            super(itemView);
//        }
//    }
//
//    private class TestViewHolder extends RecyclerView.ViewHolder {
//
//        TextView content;
//
//        public TestViewHolder(View itemView) {
//            super(itemView);
//            content = (TextView) itemView.findViewById(R.id.content);
//
//        }
//    }
}
